
package cn.ad.server.web.servlet;

import cn.ad.server.common.utils.SpringContextUtils;
import cn.ad.server.web.spring.service.actions.AdTrackingAction;
import cn.ad.server.web.spring.service.actions.WinnoticeRequestAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author dongbin.yu
 * @from 2016-05-30
 * @since V1.0
 */
@WebServlet("/imp.html")
public class AdImpServlet extends AbstractAdServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Logger logger = LoggerFactory.getLogger(AdImpServlet.class);

	public AdTrackingAction getTrackingAction() {
		return SpringContextUtils.getBean(AdTrackingAction.class);
	}

	public WinnoticeRequestAction getWinnoticeAction() {
		return SpringContextUtils.getBean(WinnoticeRequestAction.class);
	}

	public void doService(HttpServletRequest request, HttpServletResponse response) {
		
		try {
			request.getRequestDispatcher("/v2/imp/ch/1").forward(request, response);
		} catch (ServletException e) {
			logger.error("ServletException", e);
		} catch (IOException e) {
			logger.error("IOException", e);
		}
		return;
		
//		ClickAndImpressionCommand clickCommand = new ClickAndImpressionCommand();
//		WebUtils.convetGetRequestParam(request, clickCommand);
//
//		if (Exchange.GDT.getValue().equals(clickCommand.getPlatform_id())) {
//			covertGDTParam(clickCommand);
//		}
//
//		ClickPresentation cp = new ClickPresentation();
//		String ip = IPUtils.parseIp(request);
//		clickCommand.setIp(ip);
//		clickCommand.setUa(request.getHeader("user-agent"));
//		clickCommand.setImp(true);
//		clickCommand.setHandleType("2");
//		// HIS 历史代码： adService.handleClick(clickCommand, cp);
//		getTrackingAction().handleClick(clickCommand, cp);
//		
//		WinNoticePresentation wnp = null;
//		try {
//			
//			String winPrice = request.getParameter("win_price");
//			// 广点通的曝光和win notice合在一个接口中处理
//			if (!StringUtils.isEmpty(winPrice)) {
//				WinNoticeCommand wnc = new WinNoticeCommand();
//				wnc.setAdxType(Integer.valueOf(Exchange.GDT.getValue()));
//
//				wnc.setWin_price(winPrice);
//				wnc.setCid(clickCommand.getCid());
//				wnc.setGuid(clickCommand.getGuid());
//				wnc.setIp(clickCommand.getIp());
//				wnc.setMid(clickCommand.getMid());
//				wnc.setPlatform_id(clickCommand.getPlatform_id());
//				wnc.setSid(clickCommand.getSid());
//				wnc.setUa(clickCommand.getUa());
//				wnp = getWinnoticeAction().makeWinNotice(wnc);
//			}
//		} catch (Exception e) {
//			logger.error("/imp.html win-notice error:{}", e.getMessage(), e);
//		} finally {
//			adService.unbond();
//			loggerService.logAdImpression(cp);
//			if (wnp != null)
//				loggerService.logWinNotice(wnp);
//			
//		}
//
//		response.setContentType("image/jpg");
//
//		try (InputStream is = AdImpServlet.class.getResourceAsStream("/1x1.png");
//				ServletOutputStream os = response.getOutputStream()) {
//			byte[] b = new byte[1024];
//			int len = 0;
//			while ((len = is.read(b)) != -1) {
//				os.write(b, 0, len);
//			}
//			os.flush();
//		}catch(IOException e) {
//			logger.warn("写入响应出现IO异常, 请求IP, {}, 请求URL{}, 异常原因{} ", ip, request.getRequestURL(), e.getMessage());
//		}
	}
}
